package org.example.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Pair;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.common.configs.exception.WGException;
import org.example.common.configs.log.LogAdvice;
import org.example.common.configs.permission.Permission;
import org.example.common.enums.ConfirmedStateEnum;
import org.example.common.session.SessionStore;
import org.example.common.session.UserContext;
import org.example.common.support.APIResponse;
import org.example.common.enums.WGErrorEnum;
import org.example.common.utils.AESEncryptUtils;
import org.example.listener.CurvePlanChangedEventListener;
import org.example.listener.event.CurvePlanChangedEvent;
import org.example.common.configs.queue.disruptor.DisruptorEventProducer;
import org.example.model.dto.AuditContentParamDto;
import org.example.model.dto.FullUserDto;
import org.example.model.entity.SysConfig;
import org.example.service.testSendMailService;
import org.mockito.internal.util.collections.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Set;

/**
 * @author weigang
 * @create 2022-07-08 18:02
 */

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/testdemo")
@EnableAsync
@Permission(value = "WG")
public class testController {

    @Autowired
    private CurvePlanChangedEventListener curvePlanChangedEventListener;

    /* @Autowired
    private ClassMapper classMapper; */

    private final DisruptorEventProducer disruptorEventProducer;
    private testSendMailService testSendMailService;
    private final SysConfig sysConfig;
    @Autowired
    private SessionStore sessionStore;

    @PostMapping("/test1")
    @LogAdvice(desc = "test disurptor handle event", operateType = "query")
    public void testDisruptor1(){
        // 消费数据
        curvePlanChangedEventListener.handleCurvePlanChanged(new CurvePlanChangedEvent("weigang"));
    }

    @PostMapping("/test2")
    @LogAdvice(desc = "test disurptor publish event", operateType = "query")
    public void testDisruptor2(){
        // 发布事件，生产数据
        disruptorEventProducer.publishEvent(new CurvePlanChangedEvent("weigang"));
    }

    @PostMapping("/test3")
    @LogAdvice(desc = "test WGException ", operateType = "query")
    public APIResponse<String> test3(){

        Assert.isTrue(!Objects.equals(1, 2),
                () -> new WGException(WGErrorEnum.e_1241));

        return APIResponse.ok("ok");
    }

/*
    @PostMapping("/test4")
    @LogAdvice(desc = "test4", operateType = "query")
    public APIResponse<List<Class>> test4(){

        List<Class> classes = classMapper.selectList(null);
        classes.forEach(System.out::println);

        return APIResponse.ok(classes);
    }
*/

    @PostMapping("/test5")
    @LogAdvice(desc = "test Send Mail ", operateType = "query")
    public APIResponse<String> testSendMail(){
        testSendMailService.testSendCheckMail();
        testSendMailService.testSendCommitMail();
        return APIResponse.ok("ok");
    }

    /**
     * 接受审核邮件，跳转审核接口
     *
     * @param request
     * @param auditFlag
     * @return Response
     */
    @LogAdvice(desc = "test Check Mail ", operateType = "query")
    @GetMapping("/preAudit/{auditFlag}")
    public APIResponse<String> preAudit(HttpServletRequest request, @PathVariable String auditFlag){

        AuditContentParamDto auditContentParam = new AuditContentParamDto();
        if (Objects.equals("success", auditFlag)) {
            auditContentParam.setConfirmState(ConfirmedStateEnum.CONFIRMED.getState());
        } else {
            auditContentParam.setConfirmState(ConfirmedStateEnum.CONFIRM_REJECTED.getState());
        }

        String classId = new String(Objects.requireNonNull(AESEncryptUtils.decrypt(AESEncryptUtils.parseHexStr2Byte(request.getParameter("cpi")), sysConfig.getAesKey())));
        String className = new String(Objects.requireNonNull(AESEncryptUtils.decrypt(AESEncryptUtils.parseHexStr2Byte(request.getParameter("ic")), sysConfig.getAesKey())));

        Set<String> classIdsSet = Sets.newSet();
        classIdsSet.add(classId);
        auditContentParam.setClassIds(classIdsSet);

        Pair<String, FullUserDto> sessionPair = sessionStore.getUser(null, classId);
        UserContext.putUser(sessionPair);

        disruptorEventProducer.publishEvent(new CurvePlanChangedEvent("weigang"));

        sessionStore.clearUser(sessionPair.getKey());
        UserContext.clear();

        return APIResponse.ok();
    }

}
